"""
Case Type   : 资源池化系统视图
Case Name   : 默认配置下，执行switchover操作，查看系统视图pg_total_memory_detail
Create At   : 2025/2
Owner       : haomeng
Description :
    1、查看内存保护开关
    2、主节点查看系统表pg_total_memory_detail & gs_total_memory_detail中DMSDrcContext字段是否存在
    3、备节点查看系统表pg_total_memory_detail & gs_total_memory_detail中DMSDrcContext字段是否存在
    4、执行switchover操作切换主备节点
    5、主节点查看系统表pg_total_memory_detail & gs_total_memory_detail中DMSDrcContext字段是否存在
    6、备节点查看系统表pg_total_memory_detail & gs_total_memory_detail中DMSDrcContext字段是否存在
    7、恢复初始环境
Expect      :
    1、参数默认值为off
    2、无相关信息
    3、无相关信息
    4、切换成功
    5、无相关信息
    6、无相关信息
    7、环境恢复成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class ShareStorageSystemView(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta_sh = CommonSH('Standby1DbUser')
        self.constant = Constant()

        self.memory_limit = 'enable_memory_limit'
        self.sys_view = ['pg_total_memory_detail', 'gs_total_memory_detail']

    def test_ss_system_view(self):
        text = '-----step1:查看内存保护开关;  expect:默认值为on-----'
        self.log.info(text)

        value = self.pri_sh.execut_db_sql(f'show {self.memory_limit}')
        self.log.info(value)
        self.assertEqual('on', value.splitlines()[-2].strip(), '参数默认值校验失败')

        text = '-----step2:主节点查看系统表pg_total_memory_detail & gs_total_memory_detail' \
               '中DMSDrcContext字段是否存在  expect:无相关信息-----'
        self.log.info(text)

        for view in self.sys_view:
            sql1 = f"select * from {view} where memorytype='DMSDrcContext';"
            self.log.info(sql1)
            res1 = self.pri_sh.execut_db_sql(sql1)
            self.log.info(res1)
            self.assertIn('(0 rows)', res1, '存在异常信息')

        text = '-----step3:备节点查看系统表pg_total_memory_detail & gs_total_memory_detail' \
               '中DMSDrcContext字段是否存在  expect:无相关信息-----'
        self.log.info(text)

        for view in self.sys_view:
            sql2 = f"select * from {view} where memorytype='DMSDrcContext';"
            self.log.info(sql2)
            res2 = self.sta_sh.execut_db_sql(sql2)
            self.log.info(res2)
            self.assertIn('(0 rows)', res2, '存在异常信息')

        text = '-----step4:执行switchover操作切换主备节点  expect:切换成功-----'
        self.log.info(text)

        switchover = self.pri_sh.exec_cm_ctl('switchover', '-n 2',
                                             f'-D {macro.DB_INSTANCE_PATH}')
        self.log.info(switchover)
        self.assertIn(self.constant.SWITCHOVER_SUCCESS_MSG, switchover, '执行失败')

        text = '-----step5:新主节点查看系统表pg_total_memory_detail & gs_total_memory_detail' \
               '中DMSDrcContext字段是否存在  expect:无相关信息-----'
        self.log.info(text)

        for view in self.sys_view:
            sql3 = f"select * from {view} where memorytype='DMSDrcContext';"
            self.log.info(sql3)
            res3 = self.sta_sh.execut_db_sql(sql3)
            self.log.info(res3)
            self.assertIn('(0 rows)', res3, '存在异常信息')

        text = '-----step6:新备节点查看系统表pg_total_memory_detail & gs_total_memory_detail' \
               '中DMSDrcContext字段是否存在  expect:无相关信息-----'
        self.log.info(text)

        for view in self.sys_view:
            sql4 = f"select * from {view} where memorytype='DMSDrcContext';"
            self.log.info(sql4)
            res4 = self.pri_sh.execut_db_sql(sql4)
            self.log.info(res4)
            self.assertIn('(0 rows)', res4, '存在异常信息')

    def tearDown(self):
        text = '-----step7:恢复初始环境  expect:恢复成功-----'
        self.log.info(text)

        restore = self.pri_sh.exec_cm_ctl('switchover', '-a')
        self.log.info(restore)
        self.assertIn(self.constant.SWITCHOVER_SUCCESS_MSG, restore, '执行失败')

        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertIn('Primary Normal', status.splitlines()[-1].split('|')[0],
                      '恢复集群初始状态失败')
        self.log.info(f'----{os.path.basename(__file__)} end----')
